package com.sky.myapplication.java.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;

import androidx.core.content.FileProvider;

import java.io.File;

/**
 * Created by Min
 * Date 2023/2/1  11:24
 */
public class FileUtils {
    //打开类型的文件
    public static Intent openFile(Context context, String filePath) {
        if (filePath == null) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists())
            return null;
        /* 取得扩展名 */
        String end = file
                .getName()
                .substring(file.getName().lastIndexOf(".") + 1,
                        file.getName().length()).toLowerCase();
        end = end.trim().toLowerCase();
//		System.out.println(end);
        /* 依扩展名的类型决定MimeType */
        if (end.equals("apk")) {
            return getApkFileIntent(context,filePath);
        } else if (end.equals("ppt")) {
            return getPptFileIntent(context,filePath);
        } else if (end.equals("xls")) {
            return getExcelFileIntent(context,filePath);
        } else if (end.equals("doc")) {
            return getWordFileIntent(context,filePath);
        } else if (end.equals("pdf")) {
            return getPdfFileIntent(context,filePath);
        }  else if (end.equals("txt")) {
            return getTextFileIntent(context,filePath, false);
        } else {
            return getAllIntent(context,filePath);
        }
    }
    // Android获取一个用于打开APK文件的intent
    public static Intent getApkFileIntent(Context context,String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(new File(param)), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }
    // Android获取一个用于打开PPT文件的intent
    public static Intent getPptFileIntent(Context context,String param) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        Uri uri = Uri.fromFile(new File(param));
        Uri uri = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        return intent;
    }
    // Android获取一个用于打开Excel文件的intent
    public static Intent getExcelFileIntent(Context context,String param) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        Uri uri = Uri.fromFile(new File(param));
        Uri uri = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
    }
    // Android获取一个用于打开Word文件的intent
    public static Intent getWordFileIntent(Context context,String param) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        Uri uri = Uri.fromFile(new File(param));
        Uri uri = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }
    // Android获取一个用于打开PDF文件的intent
    public static Intent getPdfFileIntent(Context context,String param) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        Uri uri = Uri.fromFile(new File(param));
        Uri uri = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
        intent.setDataAndType(uri, "application/pdf");
        return intent;

//        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
//        intent.addCategory(Intent.CATEGORY_OPENABLE);
//        intent.setType("application/pdf");
//        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
//        context.startActivityForResult(intent, PICK_PDF_FILE);

    }
    // Android获取一个用于打开文本文件的intent
    public static Intent getTextFileIntent(Context context,String param, boolean paramBoolean) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (paramBoolean) {
            Uri uri1 = Uri.parse(param);
            intent.setDataAndType(uri1, "text/plain");
        } else {
//            Uri uri2 = Uri.fromFile(new File(param));
            Uri uri2 = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
            intent.setDataAndType(uri2, "text/plain");
        }
        return intent;
    }
    // Android获取一个用于打开APK文件的intent
    public static Intent getAllIntent(Context context,String param) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                Uri uri = null;
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                uri = Uri.fromFile(new File(param));
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.setDataAndType(uri, "text/plain");
                intent.setDataAndType(uri, "*/*");
                return intent;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } else {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
//        Uri uri = Uri.fromFile(new File(param));
            Uri uri = FileProvider.getUriForFile(context, "com.line.testapplication.fileProvider", new File(param));
            intent.setDataAndType(uri, "*/*");
            return intent;
        }

    }
}

